package com.example.scity.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.scity.APIInterface;
import com.example.scity.AixinjuanzengActivity;
import com.example.scity.CitymetorActivity;
import com.example.scity.Configs;
import com.example.scity.CwyyActivity;
import com.example.scity.HdglActivity;
import com.example.scity.HouseActivity;
import com.example.scity.JobActivity;
import com.example.scity.MovieActivity;
import com.example.scity.MzyyActivity;
import com.example.scity.ParkActivity;
import com.example.scity.R;
import com.example.scity.ShjfActivity;
import com.example.scity.SjfxActivity;
import com.example.scity.WlcxActivity;
import com.example.scity.WmdcActivity;
import com.example.scity.ZhihuiJGActivity;
import com.example.scity.beens.ServiceListBeen;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Fragment2 extends Fragment {
    View view;
    ListView listView;
    RecyclerView recyclerView;
    ServiceListBeen serviceList;
    class ServiceLists {
        public String type;
        public int sort;
        public List<ServiceListBeen.Rows> rows;
    }
    List<ServiceLists> serviceLists = new ArrayList<>();
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 2:
                    sort();
                    listView.setAdapter(new BaseAdapter() {
                        @Override
                        public int getCount() {
                            return serviceLists.size();
                        }

                        @Override
                        public Object getItem(int position) {
                            return serviceLists.get(position);
                        }

                        @Override
                        public long getItemId(int position) {
                            return position;
                        }

                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_fm2_list,null);
                            TextView textView = convertView.findViewById(R.id.item_fm2_textview);
                            textView.setText(serviceLists.get(position).type);
                            return convertView;
                        }
                    });
                    break;
            }
        }
    };


    private void sort() {
        int i,j;
        serviceLists.clear();
        for (i=0;i<serviceList.rows.size();i++){
            for(j=0;j<serviceLists.size();j++){
                if(serviceList.rows.get(i).serviceType.equals(serviceLists.get(j).type)){
                    serviceLists.get(j).rows.add(serviceList.rows.get(i));
                    break;
                }
            }
            //新的类别
            if(j==serviceLists.size()){
                ServiceLists s1= new ServiceLists();
                s1.rows = new ArrayList<>();
                s1.sort=serviceList.rows.get(i).sort;
                s1.type = serviceList.rows.get(i).serviceType;
                serviceLists.add(s1);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_2,container,false);

        return view;
    }
    View lastview;
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = view.findViewById(R.id.fm2_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                displayRecy(position);
                view.setBackgroundColor(Color.LTGRAY);
                if(lastview!=null){
                    lastview.setBackgroundColor(Color.WHITE);
                }
                lastview=view;
            }
        });
        recyclerView = view.findViewById(R.id.fm2_rclist);

        loadServiceList();
    }

    private void displayRecy(int p) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),3);
        recyclerView.setLayoutManager(gridLayoutManager);
        //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_servilist,null);
                return new Fragment1.ViewHodler1(view);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                int p2=position;
                Fragment1.ViewHodler1 viewHodler = (Fragment1.ViewHodler1) holder;
                Glide.with(getActivity()).load(Configs.Host+serviceLists.get(p).rows.get(position).imgUrl).into(viewHodler.imageView);
                viewHodler.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (serviceLists.get(p).rows.get(p2).sort){
                            case 0:
                                //Toast.makeText(getContext(),"停哪儿",Toast.LENGTH_LONG).show();
                                startActivity(new Intent(getActivity(), ParkActivity.class));
                                break;
                            case 1:
                                startActivity(new Intent(getActivity(), CitymetorActivity.class));
                                //Toast.makeText(getContext(),"城市地铁",Toast.LENGTH_LONG).show();
                                break;
                            case 11:
                                startActivity(new Intent(getActivity(), HdglActivity.class));
                                //Toast.makeText(getContext(),"活动管理",Toast.LENGTH_LONG).show();
                                break;
                            case 12:
                                //   startActivity(new Intent(getActivity(), CwyyActivity.class));
                                  Toast.makeText(getContext(),"宠物医院",Toast.LENGTH_LONG).show();
                                break;
                            case 13:
                                //  startActivity(new Intent(getActivity(), WlcxActivity.class));
                                Toast.makeText(getContext(),"物流查询",Toast.LENGTH_LONG).show();
                                break;
                            case 14:
                                // startActivity(new Intent(getActivity(), AixinjuanzengActivity.class));
                                Toast.makeText(getContext(),"爱心捐赠",Toast.LENGTH_LONG).show();
                                break;
                            case 15:
                                Toast.makeText(getContext(),"青年驿站",Toast.LENGTH_LONG).show();
                                break;
                            case 16:
                                startActivity(new Intent(getActivity(), SjfxActivity.class));
                                //Toast.makeText(getContext(),"数据分析",Toast.LENGTH_LONG).show();
                                break;
                            case 3:
                                 startActivity(new Intent(getActivity(), MzyyActivity.class));
                                // Toast.makeText(getContext(),"门诊预约",Toast.LENGTH_LONG).show();
                                break;
                            case 5:
                                startActivity(new Intent(getActivity(), ZhihuiJGActivity.class));
                               // Toast.makeText(getContext(),"智慧交管",Toast.LENGTH_LONG).show();
                                break;
                            case 6:
                                startActivity(new Intent(getActivity(), ShjfActivity.class));
                                //Toast.makeText(getContext(),"生活缴费",Toast.LENGTH_LONG).show();
                                break;
                            case 7:
                                startActivity(new Intent(getActivity(), WmdcActivity.class));
                                //Toast.makeText(getContext(),"外卖订餐",Toast.LENGTH_LONG).show();
                                break;
                            case 8:
                                startActivity(new Intent(getActivity(), HouseActivity.class));
                                //Toast.makeText(getContext(),"找房子",Toast.LENGTH_LONG).show();
                                break;
                            case 9:
                                startActivity(new Intent(getActivity(), MovieActivity.class));
                                //Toast.makeText(getContext(),"看电影",Toast.LENGTH_LONG).show();
                                break;
                            case 10:
                                startActivity(new Intent(getActivity(), JobActivity.class));
                                //Toast.makeText(getContext(),"找工作",Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                });
                viewHodler.textView.setText(serviceLists.get(p).rows.get(position).serviceName);
            }

            @Override
            public int getItemCount() {
                return serviceLists.get(p).rows.size();
            }
        });
    }

    private void loadServiceList() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.ServiceList + "?isRecommend=N").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                serviceList = new Gson().fromJson(response.body().string(), ServiceListBeen.class);
                handler.sendEmptyMessage(2);
            }
        });
    }
}
